$('.wall').jaliswall({ item: '.article' });

//幻灯片
var slide3 = new auiSlide({
    container:document.getElementById("aui-slide3"),
    // "width":300,
    "height":200,
    "speed":500,
    "autoPlay": 3000, //自动播放
    "loop":true,
    "pageShow":true,
    "pageStyle":'line',
    'dotPosition':'center'
});

(function () {

    var api = {
        'collectUrl': '/api/collect/doCollect'
    };

    sm.controller('wx-index-controller', ['$scope', '$http', 'Toast', function ($scope, $http, Toast){

        $scope.lists = {};
        $scope.loading = true;

        $scope.loadList = function () {

            $scope.loading = true;
            $http.get('api/home/waterFlow', {
                params: {
                    page: 1
                }
            }).success(function (res) {
                $scope.loading = false;
                if (res.state) {
                    $scope.lists.total = res.data.total;
                    $scope.lists.data = res.data['data'];

                    $html = '';
                    angular.forEach(res.data['data'], function (val) {
                        $html += '<div class="article aui-padded-b-5">'
                            + '<a class="gu-water-flow" href="/detail?id='+ val.goodsId +'">'
                            + '<img src="' + val.thumb + '" onerror="" />'
                            + '<h4 class="aui-ellipsis-2">'+ val.goodsName +'</h4>'
                            + '</a>'
                            + '<p class="w-footer"><small class="gu-w-price">'+'￥ ' + val.price+'</small>' +
                            '<i class="aui-iconfont aui-icon-like aui-pull-right gu-w-like '+ (val.collected == true ? 'active' : '') +'" data-goodsid="'+val.goodsId+'" ' +
                            'data-status="' + (val.collected ? 1 : 0)+'"></i></p>'
                            + '</div>'
                    });

                    $('.wall').html($html);

                    setTimeout(function () {
                        $('.wall').jaliswall({ item: '.article' });
                        doDom();
                    });



                }
            });
        };

        function doDom() {
            $('.gu-w-like').off('click').on('click', function () {

                var goodsId = $(this).attr('data-goodsid');
                var obj = $(this);
                var nowStatus = $(this).attr('data-status');
                var newStatus = nowStatus == 1 ? 0 : 1;

                $http.post(api.collectUrl, {
                    goodsId: goodsId,
                    action: newStatus ? 'on' : 'off'
                }).success(function (res){
                    if (res.state) {
                        if (newStatus) {
                            // 收藏
                            Toast.success({title: '收藏成功'});
                            obj.addClass('active');
                            obj.attr('data-status', 1);
                        } else {
                            // 取消
                            Toast.success({title: '取消成功'});
                            obj.removeClass('active');
                            obj.attr('data-status', 0);
                        }

                    } else {
                        Toast.error({
                            title: '收藏失败'
                        });
                    }
                });
            });
        }

        $scope.loadList();

    }]);
})();

